package dcedit.util;

// Import Java SDK Classes
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import java.awt.Color;

/** IntegerSpinner class */
public class IntegerSpinner extends JSpinner {
   /** Serializable constant */
   public static final long serialVersionUID = 1281649017897L;

   /** The label which is associated with this spinner */
   private JLabel label = new JLabel();

   /** constructor */
   public IntegerSpinner() {
      this(new String(), null, null);
   } // END constructor

   /** constructor */
   public IntegerSpinner(String label) {
      this(label, null, null);
   } // END constructor

   /** constructor */
   public IntegerSpinner(Color foregroundColor, Color backgroundColor) {
      this(new String(), foregroundColor, backgroundColor);
   } // END constructor

   /** constructor */
   public IntegerSpinner(String label, Color foregroundColor, Color backgroundColor) {
      super(new SpinnerNumberModel(0, 0, 100, 1));

      this.setLabel(label);

      if(null != foregroundColor && null != backgroundColor) {
         this.setForeground(foregroundColor);
         this.setBackground(backgroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).setForeground(foregroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).setBackground(backgroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).getTextField().setForeground(foregroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).getTextField().setBackground(backgroundColor);
      } // END if

      ((JSpinner.DefaultEditor)this.getEditor()).getTextField().setEditable(false);
   } // END constructor

   @Override
   /** setForeground method */
   public void setForeground(Color foregroundColor) {
      if(null != foregroundColor) {
         super.setForeground(foregroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).setForeground(foregroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).getTextField().setForeground(foregroundColor);
      } // END if
   } // END setForeground

   @Override
   /** setBackground method */
   public void setBackground(Color backgroundColor) {
      if(null != backgroundColor) {
         super.setBackground(backgroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).setBackground(backgroundColor);
         ((JSpinner.DefaultEditor)this.getEditor()).getTextField().setBackground(backgroundColor);
      } // END if
   } // END setForeground

   /** getLabel method */
   public JLabel getLabel() {
      return label;
   } // END getLabel

   /** setLabel method */
   public void setLabel(String label) {
      if(null != label) {
         this.label = new JLabel(label);
         this.label.setForeground(this.getForeground());
         this.label.setBackground(this.getBackground());
      } // END if
   } // END setLabel

   /** getNumber method */
   public int getNumber() {
      return ((SpinnerNumberModel)this.getModel()).getNumber().intValue();
   } // END getNumber

   /** setNumber method */
   public void setNumber(int num) {
      this.getModel().setValue(Integer.valueOf(num));
   } // END setNumber

   /** resetNumber method */
   public void resetNumber() {
      this.setNumber(0);
   } // END resetNumber
} // END class IntegerSpinner